import { fun } from "@/common/index";

/**
 * 自定义错误处理类
 */
export default class Exception {
  static handle(e: any) {
    if (typeof e === "string") {
      e = Error(e);
    }
    if (
      typeof e === "object" &&
      !Object.prototype.hasOwnProperty.call(e, "message")
    ) {
      if (Object.prototype.hasOwnProperty.call(e, "msg")) {
        e.message = e.msg;
      }
      if (Object.prototype.hasOwnProperty.call(e, "Message")) {
        e.message = e.Message;
      }
    }

    if (e.message === "nocare" || e.message === "require login") return;
    const { type = "toast", icon = "none" } = e;
    let { message = "错误信息" } = e;
    if (typeof e !== "object") {
      message = e;
    }
    // 开发模式下，全部都弹出来
    if (type === "toast") fun.error({ msg: message });
    if (type === "modal") fun.alert({ msg: message });
    return {
      message,
      type,
      icon
    };
  }
}
